package com.atguigu.gulimail.product.service.impl;

import com.atguigu.gulimail.common.utils.PageUtils;
import com.atguigu.gulimail.common.utils.Query;
import com.atguigu.gulimail.product.dao.CategoryDao;
import com.atguigu.gulimail.product.entity.CategoryEntity;
import com.atguigu.gulimail.product.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询所有分类信息并以树状形式返回
     */
    @Override
    public List listTree() {
        //1、查询所有分类信息
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //2、利用mybatis实现树状结构的实现
        //2.1、首先筛选出父结构体，筛选条件menu.getParentCid()== 0
        List<CategoryEntity> listTree = categoryEntities.stream().filter(
                menu -> menu.getParentCid() == 0
        ).map(
                //2.2、映射赋值，即将此menu（categoryEntitie）的属性Children赋值
                (menu) -> {
                    menu.setChildren(getListChildren(menu, categoryEntities));
                    return menu;
                }
        ).sorted(
                //2.3、排序
                (menu1, menu2) -> {
                    return (menu1.getSort() == null? 0: menu1.getSort()) - (menu2.getSort() == null? 0: menu2.getSort());
                }
        ).collect(Collectors.toList()); //2.4、收集起来转化成新数组

        return listTree;
    }

    @Override
    public void removeMenusByIds(List<Long> asList) {
        //  TODO 检查是否被别的地方引用
        baseMapper.deleteBatchIds(asList);
    }

    /**
     * 给每一个对象进行检查是否有子数组，如果有则赋值
     * @param root
     * @param listTree
     * @return
     */
    private List<CategoryEntity> getListChildren(CategoryEntity root, List<CategoryEntity> listTree) {
        List<CategoryEntity> listChildren = listTree.stream().filter(
                //判断是否有子数组，如果有，过滤出来
                (category) -> {
                    return root.getCatId() == category.getParentCid();
                }
        ).map(
                //循环判断
                (category) -> {
                    category.setChildren(getListChildren(category, listTree));
                    return category;
                }
        ).sorted(
                //排序
                (menu1, menu2) -> {
                    return (menu1.getSort() == null? 0: menu1.getSort()) - (menu2.getSort() == null? 0: menu2.getSort());
                }
        ).collect(Collectors.toList());
        return listChildren;
    }

}